package dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Set;

public class InstanceMethodProxy implements InvocationHandler{
	Object obj;
	InstanceMethodProxy (Object obj){
		this.obj=obj;
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("before call "+method.toString());
		Object ret = method.invoke(obj, args);
		System.out.println("after call "+method.toString());
		return ret;
	}
	static Object newInst(Object obj) {
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InstanceMethodProxy(obj));
	}
public static void main(String[] args) {
	Set s=(Set) InstanceMethodProxy.newInst(new HashSet<>());
	System.out.println(s.getClass());
	s.add("hello");
}
}
